/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controladores;

import Interfaces.ICtrlDocumentos;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Sebas
 */
public class CtrlDocumentos implements ICtrlDocumentos{

    @Override
    public void saveOnServer(byte archivo[], String nomArchivo, boolean esGrupo, String duenoMuro) {
        String camino = "C:\\Server\\";
        if (!esGrupo){
            camino += duenoMuro+"\\"+nomArchivo+"\\";
            try {
                FileOutputStream fos = new FileOutputStream(camino);
                fos.write(archivo);
                fos.close();
            } catch (IOException ex) {
                Logger.getLogger(CtrlDocumentos.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else {
            
        }
        
    }

    
    @Override
    public byte[] traerDoc(String nomArchivo, String mailUsr, boolean esGrupo) {
        
        String camino = "C:\\Server\\"+mailUsr+"\\"+nomArchivo;
        byte[] archCodificado = null;
        
        File archivo_server = new File(camino);

        InputStream is = null;
        try {
            is = new FileInputStream(archivo_server);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(CtrlDocumentos.class.getName()).log(Level.SEVERE, null, ex);
        }

        // Get the size of the file
        long length = archivo_server.length();

        // You cannot create an array using a long type.
        // It needs to be an int type.
        // Before converting to an int type, check
        // to ensure that file is not larger than Integer.MAX_VALUE.
        if (length > Integer.MAX_VALUE) {
            // File is too large
        }

        // Create the byte array to hold the data
        archCodificado = new byte[(int)length];

        // Read in the bytes
        int offset = 0;
        int numRead = 0;
        try {
            while (offset < archCodificado.length
                   && (numRead=is.read(archCodificado, offset, archCodificado.length-offset)) >= 0) {
                offset += numRead;
            }
        } catch (IOException ex) {
            Logger.getLogger(CtrlDocumentos.class.getName()).log(Level.SEVERE, null, ex);
        }

        // Ensure all the bytes have been read in
        if (offset < archCodificado.length) {
            try {
                throw new IOException("Could not completely read file "+archivo_server.getName());
            } catch (IOException ex) {
                Logger.getLogger(CtrlDocumentos.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        try {
            // Close the input stream and return bytes
            is.close();
        } catch (IOException ex) {
            Logger.getLogger(CtrlDocumentos.class.getName()).log(Level.SEVERE, null, ex);
        }
        return archCodificado;
    }
}
